package com.shallwead.bna;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.shallwead.bna.Async.AdClickAsync;
import com.shallwead.bna.Async.AdClickTelAsync;
import com.shallwead.bna.Async.GetFrontAdAsync;
import com.shallwead.bna.Async.PushClickAsync;
import com.shallwead.bna.Async.PushClickTelAsync;
import com.shallwead.bna.Async.TrakerAsync;
import com.shallwead.bna.dialog.AdBackDialog;
import com.shallwead.bna.dialog.AdFrontDialog;
import com.shallwead.bna.object.AdBack;
import com.shallwead.bna.object.AdClick;
import com.shallwead.bna.object.AdFront;
import com.shallwead.bna.object.AdGcmMsg;
import com.shallwead.bna.object.AdTracker;
import com.shallwead.bna.util.Constants;
import com.shallwead.bna.util.Logger;
import com.shallwead.bna.util.SharedPreferenceUtil;
import com.shallwead.bna.util.gcm.GCM_Utils;
import com.shallwead.bna.util.image.ImageDownloader;

public class BnA {

	public static int screenWidth;
	public static int screenHeight;
	public static String UDID;

	public static boolean TestMode = false;
	public static String GCM_Project_Number = "";
	public static String Analytics_Project_Number = "";

	public static AdFront adFront = new AdFront();
	public static AdClick adClick = new AdClick();
	public static AdTracker adTracker = new AdTracker();
	public static ArrayList<AdBack> adBackList = new ArrayList<AdBack>();

	public static String pkg = "";

	public static AdGcmMsg gcmMsg = null;
	public static Bitmap gcmImg = null;
	public static float iconSize = 0;
	public static ImageDownloader imgDownloader;
	public static HashMap<String, Bitmap> imgArray = new HashMap<String, Bitmap>();

	/**
	 * @param mContext
	 */
	public BnA(Context mContext) {
		getUDID(mContext);
		getDP(mContext);
		this.pkg = mContext.getPackageName();
		this.imgDownloader = new ImageDownloader(mContext);
		Logger.e(pkg);
	}

	public void getDP(Context mContext) {
		Display dis = ((WindowManager) mContext
				.getSystemService(mContext.WINDOW_SERVICE)).getDefaultDisplay();
		DisplayMetrics metrics = new DisplayMetrics();
		dis.getMetrics(metrics);
		float dpi = metrics.densityDpi;
		iconSize = 70 / (160 / dpi);

	}

	public void getUDID(Context context) {
		try {
			TelephonyManager tm = ((TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE));
			UDID = gID(tm, context);
			Log.e("UDID", UDID);
		} catch (Exception e) {
			UDID = "NOUDID";
		}
	}

	private String gID(TelephonyManager tm, Context context) {
		if (tm == null)
			return "NoTM_"
					+ Settings.Secure.getString(context.getContentResolver(),
							Settings.Secure.ANDROID_ID);
		else
			return ((tm.getDeviceId() != null) ? tm.getDeviceId() : "N")
					+ "_"
					+ ((tm.getSimSerialNumber() != null) ? tm
							.getSimSerialNumber() : "N")
					+ "_"
					+ Settings.Secure.getString(context.getContentResolver(),
							Settings.Secure.ANDROID_ID);
	}

	public static void showFrontAd(Context mContext) {
		new GetFrontAdAsync(mContext).execute();
	}

	public static void showFrontAd(Context mContext, AdFront adFront) {
		if (!isInstall(mContext, adFront.getPkg())) {
			AdFrontDialog dlg = new AdFrontDialog(mContext, adFront);
			dlg.show();
		}
	}

	public static void showBacktAd(Context mContext) {
		// new GetBackAdAsync(mContext).execute();
		AdBackDialog dlg = new AdBackDialog(mContext);
		dlg.show();
	}

	public static void showBacktAd(Context mContext,
			ArrayList<AdBack> adBackList) {
		AdBackDialog dlg = new AdBackDialog(mContext, adBackList);
		dlg.show();
	}

	public static void goAd(Context mContext, AdFront ad) {

		SharedPreferenceUtil.putStringSharedPreference(mContext, "pushidx", "");

		AdClick adClick = new AdClick(ad.getAdIDX(), pkg);
		AdTracker adTracker = new AdTracker(ad.getAdIDX(), pkg, UDID);
		if (Constants.LINK_TYPE_WEB.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);
			new TrakerAsync(mContext, ad.getUrl(), adTracker).execute();
		} else if (Constants.LINK_TYPE_TSTORE.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);

			Uri uri = Uri.parse(ad.getUrl());
			String T_Url = "tstore://MULTI_DOWNLOAD/1/" + uri.getHost()
					+ "/0/0/0/0/0CTXX";
			new AdClickAsync(mContext, T_Url, adClick).execute();

		} else if (Constants.LINK_TYPE_GOOGLEMARKET.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);
			new AdClickAsync(mContext, ad.getUrl(), adClick).execute();
		} else if (Constants.LINK_TYPE_TELEMARKETING.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);
			new AdClickTelAsync(mContext, ad.getUrl(), adClick).execute();
		}
	}

	public static void goAd(Context mContext, AdBack ad) {
		SharedPreferenceUtil.putStringSharedPreference(mContext, "pushidx", "");

		Logger.e(ad.toString());

		AdClick adClick = new AdClick(ad.getAdIDX(), pkg);
		AdTracker adTracker = new AdTracker(ad.getAdIDX(), pkg, UDID);

		if (Constants.LINK_TYPE_WEB.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);
			new TrakerAsync(mContext, ad.getUrl(), adTracker).execute();
		} else if (Constants.LINK_TYPE_TSTORE.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);

			Uri uri = Uri.parse(ad.getUrl());
			String T_Url = "tstore://MULTI_DOWNLOAD/1/" + uri.getHost()
					+ "/0/0/0/0/0CTXX";
			new AdClickAsync(mContext, T_Url, adClick).execute();

		} else if (Constants.LINK_TYPE_GOOGLEMARKET.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);
			new AdClickAsync(mContext, ad.getUrl(), adClick).execute();
		} else if (Constants.LINK_TYPE_TELEMARKETING.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);
			new AdClickTelAsync(mContext, ad.getUrl(), adClick).execute();
		}
	}

	public static void goPushAd(Context mContext, AdBack ad) {
		SharedPreferenceUtil.putStringSharedPreference(mContext, "adidx", "");

		Logger.e(ad.toString());
		AdTracker adTracker = new AdTracker(ad.getAdIDX(), pkg, UDID);
		if (Constants.LINK_TYPE_WEB.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "pushidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);
			new TrakerAsync(mContext, ad.getUrl(), adTracker).execute();
		} else if (Constants.LINK_TYPE_TSTORE.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "pushidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);

			Uri uri = Uri.parse(ad.getUrl());
			String T_Url = "tstore://MULTI_DOWNLOAD/1/" + uri.getHost()
					+ "/0/0/0/0/0CTXX";
			new PushClickAsync(mContext, T_Url, adTracker).execute();

		} else if (Constants.LINK_TYPE_GOOGLEMARKET.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "pushidx",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);
			new PushClickAsync(mContext, ad.getUrl(), adTracker).execute();
		} else if (Constants.LINK_TYPE_TELEMARKETING.equals(ad.getLinkType())) {
			SharedPreferenceUtil.putStringSharedPreference(mContext, "pushIDX",
					ad.getAdIDX());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "adpkg",
					ad.getPkg());
			SharedPreferenceUtil.putStringSharedPreference(mContext, "u", UDID);
			new PushClickTelAsync(mContext, ad.getUrl(), adTracker).execute();
		}

	}

	public static boolean isInstall(Context mContext, String pkg) {
		try {
			PackageManager pm = mContext.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(pkg.trim(),
					PackageManager.GET_META_DATA);
			return true;
		} catch (NameNotFoundException e) {
			return false;
		}
	}

	public static void initBnA(Context mContext, String GCMID, String ANALID) {
		GCM_Project_Number = GCMID;
		Analytics_Project_Number = ANALID;
		GCM_Utils.init_GCM(mContext);
		GCM_Utils.init_Anal(mContext);
		BnA bna = new BnA(mContext);
	}
}
